package cn.iocoder.yudao.module.scrm.service.phoneencryptor;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.phoneencryptor.vo.PhoneEncryptorPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.phoneencryptor.vo.PhoneEncryptorSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.phoneencryptor.PhoneEncryptorDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.phoneencryptor.PhoneEncryptorMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.PHONE_ENCRYPTOR_NOT_EXISTS;

/**
 * 客户手机加密配置 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class PhoneEncryptorServiceImpl implements PhoneEncryptorService {

    @Resource
    private PhoneEncryptorMapper phoneEncryptorMapper;

    @Override
    public Boolean createPhoneEncryptor(PhoneEncryptorSaveReqVO createReqVO) {
        // 插入
        List<PhoneEncryptorDO> phoneEncryptors = BeanUtils.toBean(createReqVO.getDetails(), PhoneEncryptorDO.class);
        // 创建时间为空，则以当前时间为插入时间
        LocalDateTime current = LocalDateTime.now();
        phoneEncryptors.forEach(
                phoneEncryptor -> {
                    if (Objects.isNull(phoneEncryptor.getCreateTime())) {
                        phoneEncryptor.setCreateTime(current);
                    }
                    Long userId = getLoginUserId();
                    // 当前登录用户不为空，创建人为空，则当前登录用户为创建人
                    if (Objects.nonNull(userId) && Objects.isNull(phoneEncryptor.getCreator())) {
                        phoneEncryptor.setCreator(userId.toString());
                    }
                    phoneEncryptorMapper.saveOrUpdateByUserId(phoneEncryptor);
                }
        );

        return true; // 返回成功状态
    }

    @Override
    public void deletePhoneEncryptor(Long id) {
        // 校验存在
        validatePhoneEncryptorExists(id);
        // 删除
        phoneEncryptorMapper.deleteById(id);
    }

    private void validatePhoneEncryptorExists(Long id) {
        if (phoneEncryptorMapper.selectById(id) == null) {
            throw exception(PHONE_ENCRYPTOR_NOT_EXISTS);
        }
    }

    @Override
    public PhoneEncryptorDO getPhoneEncryptor(Long id) {
        return phoneEncryptorMapper.selectById(id);
    }

    @Override
    public Boolean getPhoneEncryptorByUserId(Long userId) {
        return phoneEncryptorMapper.selectOne(PhoneEncryptorDO::getUserId, userId) != null;
    }

    @Override
    public PageResult<PhoneEncryptorDO> getPhoneEncryptorPage(PhoneEncryptorPageReqVO pageReqVO) {
        return phoneEncryptorMapper.selectPage(pageReqVO);
    }

}